<?php
/*
    Meta777 is Web-based AI Software.

    Copyright (C) 2013  White Rabbit

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//MySQL Query Database
function myquery($query) {
	mysql_connect(dbhost, dbuser, dbpass);
	mysql_select_db(dbname);
	$result = mysql_query($query);
	if (!mysql_errno() && @mysql_num_rows($result) > 0) {
}
else {
$result="not";
}
	mysql_close();
	return $result;
}
// MySQL Num Rows
function myrows($result) {
	$rows = @mysql_num_rows($result);
	return $rows;
}
// MySQL fetch array
function myarray($result) {
	$array = mysql_fetch_array($result);
	return $array;
}
// MySQL escape string
function myescape($query) {
	$escape = mysql_escape_string($query);
	return $escape;
}
//Initilize Session - A FUNCTION to initilize a session so session varibles cannot be set by the user.
function initSession($session) {
	if (!isset($session['initiated'])) {
		session_regenerate_id();
		$session['initiated'] = true;
	}
	return $session;
}
//Agent Session - A FUNCTION to be sure the same web browsing client is being used during a single session.
function agentSession($session,$agent) {
	$fingerprint = md5($agent . publicsecret);
	if (isset($session['HTTP_USER_AGENT'])) {
		if ($session['HTTP_USER_AGENT'] != $fingerprint) {
			die();
			exit;
		}
	} else {
		$session['HTTP_USER_AGENT'] = $fingerprint;
	}
	return $session;
}
// A FUNCTION to generate UUID tags with the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
function gen_uuid() {
    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}
// A FUNCTION to filter $_GET parameters and let only allowed parameters/formats to be returned.
function safeGet($get) {
	if (isset($get['com']) AND preg_match("/(\w|\-)+/",$get['com'])) {
		$get['com'] = $get['com'];
	}
	else { $get['com'] = "front"; }
	if (isset($get['action']) AND preg_match("/(\w|\-)+/",$get['action'])) {
		$get['action'] = $get['action'];
	}
	else { $get['action'] = "view"; }
	if (isset($get['itemid']) AND preg_match("/\d/",$get['itemid']) AND $get['itemid'] < 18446744073709551615) {
		$get['itemid'] = $get['itemid'];
	}
	else { $get['itemid'] = 0; }
	if (isset($get['id']) AND preg_match("/\d/",$get['id']) AND $get['id'] < 18446744073709551515) {
		$get['id'] = $get['id'];
	}
	else { $get['id'] = 1; }
	if (isset($get['switch']) AND preg_match("/\w/",$get['switch'])) {
		$get['switch'] = $get['switch'];
	}
	else { $get['switch'] = "not"; }
	if (isset($get['skip']) AND preg_match("/\d/",$get['skip']) AND $get['skip'] < 18446744073709551515) {
		$get['skip'] = $get['skip'];
	}
	else { $get['skip'] = 0; }
	if (isset($get['refid']) AND preg_match("/\d/",$get['refid']) AND $get['refid'] < 18446744073709551615) {
		$get['refid'] = $get['refid'];
	}
	else { $get['refid'] = 0; }
if (isset($get['ob1id']) AND preg_match("/\d/",$get['ob1id']) AND $get['ob1id'] < 18446744073709551615) {
		$get['ob1id'] = $get['ob1id'];
	}
	else { $get['ob1id'] = 0; }
if (isset($get['ob2id']) AND preg_match("/\d/",$get['ob2id']) AND $get['ob2id'] < 18446744073709551615) {
		$get['ob2id'] = $get['ob2id'];
	}
	else { $get['ob2id'] = 0; }
	if (isset($get['ob3id']) AND preg_match("/\d/",$get['ob3id']) AND $get['ob3id'] < 18446744073709551615) {
		$get['ob3id'] = $get['ob3id'];
	}
	else { $get['ob3id'] = 0; }
if (isset($get['ob4id']) AND preg_match("/\d/",$get['ob4id']) AND $get['ob4id'] < 18446744073709551615) {
		$get['ob4id'] = $get['ob4id'];
	}
	else { $get['ob4id'] = 0; }
if (isset($get['ob1']) AND preg_match("/\w/",$get['ob1'])) {
		$get['ob1'] = $get['ob1'];
	}
	else { $get['ob1'] = "not"; }
if (isset($get['ob2']) AND preg_match("/\w/",$get['ob2'])) {
		$get['ob2'] = $get['ob2'];
	}
	else { $get['ob2'] = $itt['$varkey']['master']['def']['ob2']; }
	if (isset($get['ob3']) AND preg_match("/\w/",$get['ob3'])) {
		$get['ob3'] = $get['ob3'];
	}
	else { $get['ob3'] = "not"; }
if (isset($get['ob4']) AND preg_match("/\w/",$get['ob4'])) {
		$get['ob4'] = $get['ob4'];
	}
	else { $get['ob4'] = "not"; }
	return $get;
}
// A FUNCTION to retrieve Dynamically Stored Variables from the Database
function getLocalConfig($object) {
	$foo = FALSE;	
	$result = myquery("SELECT * FROM `".dbprfx."config` WHERE `config-object-name` = '".$object."'");
	if (myrows($result) >= 1) {
		$x = 0;
		while ($x < myrows($result)) {
			$array = myarray($result);
			if ($array['config-text-value'] != NULL) {
				if ($array['config-key-3'] != "not") {
					$k1 = $array['config-key-1'];
					$k2 = $array['config-key-2'];
					$k3 = $array['config-key-3'];
					$foo[$k1][$k2][$k3] = $array['config-text-value'];				
				}
				elseif ($array['config-key-2'] != "not") {
					$k1 = $array['config-key-1'];
					$k2 = $array['config-key-2'];
					$foo[$k1][$k2] = $array['config-text-value'];
				}
				else {
					$k1 = $array['config-key-1'];
					$foo[$k1] = $array['config-text-value'];
				}			
			}
			elseif ($array['config-num-value-u'] != 18446744073709551615) {
				if ($array['config-key-3'] != "not") {
					$k1 = $array['config-key-1'];
					$k2 = $array['config-key-2'];
					$k3 = $array['config-key-3'];
					$foo[$k1][$k2][$k3] = $array['config-num-value-u'];				
				}
				elseif ($array['config-key-2'] != "not") {
					$k1 = $array['config-key-1'];
					$k2 = $array['config-key-2'];
					$foo[$k1][$k2] = $array['config-num-value-u'];
				}
				else {
					$k1 = $array['config-key-1'];
					$foo[$k1] = $array['config-num-value-u'];
				}
			}
			elseif ($array['config-num-value'] != -9223372036854775808) {
				if ($array['config-key-3'] != "not") {
					$k1 = $array['config-key-1'];
					$k2 = $array['config-key-2'];
					$k3 = $array['config-key-3'];
					$foo[$k1][$k2][$k3] = $array['config-num-value'];				
				}
				elseif ($array['config-key-2'] != "not") {
					$k1 = $array['config-key-1'];
					$k2 = $array['config-key-2'];
					$foo[$k1][$k2] = $array['config-num-value'];
				}
				else {
					$k1 = $array['config-key-1'];
					$foo[$k1] = $array['config-num-value'];
				}
			}
			else {
				if ($array['config-key-3'] != "not") {
					$k1 = $array['config-key-1'];
					$k2 = $array['config-key-2'];
					$k3 = $array['config-key-3'];
					$foo[$k1][$k2][$k3] = NULL;				
				}
				elseif ($array['config-key-2'] != "not") {
					$k1 = $array['config-key-1'];
					$k2 = $array['config-key-2'];
					$foo[$k1][$k2] = NULL;
				}
				else {
					$k1 = $array['config-key-1'];
					$foo[$k1] = NULL;
				}			
			}
			$x++;			
		}
			
	}	
	return $foo;
}
// A FUNCTION to render formatted time in a more human readable form.
function getPrettyTime($foo) {
 return date('l, F jS, Y g:i A',$foo);
}
// A FUNCTION to render formatted text using SafeMark Markup (CALLABLE FUNCTION)
function mysimpletextdecode($itt,$foo) {
	$foo = mysimpletextdecodeinner($itt,mysimpletextdecodeinner($itt,mysimpletextdecodeinner($itt,$foo)));
	return $foo;

}
// A FUNCTION to render formatted text using SafeMark Markup (NON-CALLABLE FUNCTION)
function mysimpletextdecodeinner($itt,$foo) {
$foo = stripslashes($foo);
	$foo = str_replace("(", "&#040", $foo);
	$foo = str_replace(")", "&#041", $foo);
	$foo = str_replace("|", "&#124", $foo);
	$foo = str_replace("?", "~~~", $foo);
	$foo = str_replace(".", "<~~>", $foo);
	
	$foo = "<p>".$foo;
	
	
	
	$foo = preg_replace("/\n\n\n/","",$foo);
	$foo = preg_replace("/\n\n/","",$foo);
	$foo = preg_replace("/\n\v/","",$foo);
	$foo = preg_replace("/\n/","<p>",$foo);
	$foo = preg_replace("/\[\[(\x62|\B)+\]\]/","<strong>",$foo);
	$foo = preg_replace("/\[\[\/(\x62|\B)+\]\]/","</strong>",$foo);
	$foo = preg_replace("/\[\[(\i|\I)+\]\]/","<em>",$foo);
	$foo = preg_replace("/\[\[(list::b|LIST::B|list::B|LIST::b)+\]\]/","<ul id=\"tc\">",$foo);
	$foo = preg_replace("/\[\[(list::#|LIST::#|list::#|LIST::#)+\]\]/","<ol id=\"tc\">",$foo);
	$foo = preg_replace("/\[\[(\/list::b|\/LIST::B|\/list::B|\/LIST::b)+\]\]/","</ul>",$foo);
	$foo = preg_replace("/\[\[(\/list::#|\/LIST::#|\/list::#|\/LIST::#)+\]\]/","</ol>",$foo);
	$foo = preg_replace("/\[\[\<\~\~\>\]\]/","<li id=\"tc\">",$foo);
	$foo = preg_replace("/\[\[\/(\i|\I)+\]\]/","</em>",$foo);
	$foo = preg_replace("/\[\[(\x75|\x55)+\]\]/","<span style=\"text-decoration: underline;\">",$foo);
	$foo = preg_replace("/\[\[\/(\x75|\x55)+\]\]/","</span>",$foo);
	$foo = preg_replace("/\[\[(\h1|\H1)+\]\]/","<h1>",$foo);
	$foo = preg_replace("/\[\[\/(\h1|\H1)+\]\]/","</h1>",$foo);
	$foo = preg_replace("/\[\[(\h2|\H2)+\]\]/","<h2>",$foo);
	$foo = preg_replace("/\[\[\/(\h2|\H2)+\]\]/","</h2>",$foo);
	$foo = preg_replace("/\[\[(\h3|\H3)+\]\]/","<h3>",$foo);
	$foo = preg_replace("/\[\[\/(\h3|\H3)+\]\]/","</h3>",$foo);
	$foo = preg_replace("/\[\[(quoteright|QUOTERIGHT)+\]\]/","<div align=\"center\" style=\"float: right; width: 250px; margin: 18px; line-height: 28px; font-size: 180%; font-family: Nimbus Roman No9 L, Georgia, times, serif; color: #888;\">&ldquo; ",$foo);
	$foo = preg_replace("/\[\[\/(quoteright|QUOTERIGHT)+\]\]/"," &rdquo;</div>",$foo);
	$foo = preg_replace("/\[\[(quoteleft|QUOTELEFT)+\]\]/","<div align=\"center\" style=\"float: left; width: 250px; margin: 18px; line-height: 28px; font-size: 180%; font-family: Nimbus Roman No9 L, Georgia, times, serif; color: #888;\">&ldquo; ",$foo);
	$foo = preg_replace("/\[\[\/(quoteleft|QUOTELEFT)+\]\]/"," &rdquo;</div>",$foo);
	$foo = preg_replace("/\[\[(small|SMALL)+\]\]/","<span style=\"font-size: 75%;\">",$foo);
	$foo = preg_replace("/\[\[\/(small|SMALL)+\]\]/","</span>",$foo);
	$foo = preg_replace("/\[\[(link=|LINK=)+/","<a target=\"_BLANK\" href='",$foo);
	$foo = preg_replace("/\[\[\*\]\]/","&bull;",$foo);
	
	$foo = preg_replace("/\[\[\/(link|LINK)+\]\]/","</a> <img src=\"media/images/external.png\" border=\"0\" />",$foo);
	$foo = str_replace("&#124", "|", $foo);
	$foo = str_replace("&#040", "(", $foo);
	$foo = str_replace("&#041", ")", $foo);
	$foo = html_entity_decode($foo,ENT_QUOTES);
	
	
	
	$foo = preg_replace("/\<p\>\<div/", "<div", $foo);
	$foo = preg_replace("/\<p\>\<ol/", "<ol", $foo);
	$foo = preg_replace("/\<p\>\<\/ol/", "</ol", $foo);
	$foo = preg_replace("/\]\]/","'>",$foo);
	$foo  = str_replace("~~~", "?", $foo);
	$foo  = str_replace("<~~>", ".", $foo);
	$foo = str_replace("[[*]]", "&bull;", $foo);
	return $foo;
}
function splitUserInfo($foo) {
	
	$temp1=explode("~",$foo);
	$output['UUID'] = $temp[0];
	$output['First'] = $temp1[1];
	$output['Last'] = $temp1[2];
	$output['level'] = $temp1[3];
	$output['lang'] = $temp1[4];
	$output['gender'] = $temp1[5];
return $output;
}
?>
